package com.jwb.config.security;

import com.jwb.filter.CodeFilter;
import com.jwb.util.Md5Str;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.Md4PasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter {

    @Autowired
    private CodeFilter codeFilter;

    @Autowired
    private SuccessHandler successHandler;

    @Autowired
    private FailureHandler failureHandler;

    @Autowired
    private SecurityUserDetailsService securityUserDetailsService;

    @Autowired
    private SecurityEncoder securityEncoder;


    // https://blog.csdn.net/liuerchong/article/details/118255885
//    @Bean
//    public UserDetailsService userDetailsService(){
//        return new SecurityUserDetailsService();
//    }

    //密码编码器：
//    @Bean
//    public PasswordEncoder passwordEncoder(){
//        不加密
//        return NoOpPasswordEncoder.getInstance();
//        return new SecurityEncoder();
//
//    }

    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider(){
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
        daoAuthenticationProvider.setUserDetailsService(securityUserDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(securityEncoder);
        return daoAuthenticationProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(daoAuthenticationProvider());
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.addFilterBefore(codeFilter, UsernamePasswordAuthenticationFilter.class)// 用户名密码之前核查验证码
          .authorizeRequests()    //授权配置
        .antMatchers(excludePath()).permitAll()
        .anyRequest().authenticated()
        .and().formLogin()      //允许表单登录
        .loginPage("/index")
        .loginProcessingUrl("/authentication/form")
//        .successForwardUrl("/test/loginSuccess")   // 设置登录成功页面
        .successHandler(successHandler)
        .failureHandler(failureHandler)         // 设置登录失败处理器
        .and().logout().permitAll()         //登出路径放行
        .and().headers().frameOptions().sameOrigin()    //允许iframe内嵌
        .and().csrf().disable();        //关闭跨域伪造检查
        
    }


    private String[] excludePath(){
        return new String[]{
            "/index",
            "/checkCode",
            "/compareCode",
            "/pub/login/**",
            "/index.html",
            "/plugin/**",
            "/js/**"
        };
    }


}
